#!/bin/sh

# 用于处理hive数据同步导tidb
BASE_DIR="$(cd "$(dirname "$0")"; pwd)"
echo -e "Base Directory: ${BASE_DIR} \n"

tidb_host='{{ params.tidb_host }}'
tidb_port='{{ params.tidb_port }}'
tidb_url='{{ params.tidb_url }}'
tidb_user='{{ params.tidb_user }}'
tidb_password='{{ params.tidb_password }}'

#Tidb库名
tidb_db='{{ params.tidb_db }}'
#Tidb表名
tidb_tb='{{ params.tidb_tb }}'

#Hive库名表名
hive_db='{{ params.hive_db }}'
hive_tbl='{{ params.hive_tbl }}'

interval_dt='{{ params.interval_dt }}'
sqoop_maps='{{ params.sqoop_maps }}'

# +-------------------------
# |   计算60天涉及的月份
# +-------------------------
tidb_partition_date="{{ execution_date | cst_ds }}"

# 声明一个空数组来保存月份
months=()
# 循环60次，每次递减一天
for ((i=0; i<interval_dt; i++)); do
  # 计算当前日期
  current_date=$(date -d "$tidb_partition_date -$i days" +%Y-%m-%d)

  # 提取月份
  month=$(date -d "$current_date +1 month" +%Y%m01)

  # 判断月份是否已经存在于数组中
  if [[ ! " ${months[@]} " =~ " ${month} " ]]; then
    # 将月份添加到数组中
    months+=($month)
  fi
done

partition_str=""
for month in "${months[@]}"; do
  partition_str="$partition_str,p$month"
done
# 去掉第一位
partition_str="${partition_str:1}"
echo "$partition_str"

last_format_month=$(date -d "${months[0]}" +%Y-%m-01)
# 手动获取数组长度,原方法会引发jinja2报错
arr_length=0
for element in "${months[@]}"; do
  ((arr_length++))
done
first_format_month=$(date -d "${months[arr_length-1]}" +%Y-%m-01)

echo "first_month=$first_format_month"
echo "last_month=$last_format_month"

# +-------------------------
# |   删除60天数据
# +-------------------------
echo mysql -h ${tidb_host} -P ${tidb_port}  -u ${tidb_user} -p ****** -e "ALTER TABLE ${tidb_db}.${tidb_tb} TRUNCATE PARTITION $partition_str;"

echo `mysql -h ${tidb_host} -P ${tidb_port}  -u ${tidb_user} -p${tidb_password}  \
-e "ALTER TABLE ${tidb_db}.${tidb_tb} TRUNCATE PARTITION $partition_str;"`

# 重分区, 避免TIDB热点
echo mysql -h ${tidb_host} -P ${tidb_port}  -u ${tidb_user} -p${tidb_password}  \
-e "split partition table ${tidb_db}.${tidb_tb} PARTITION ($partition_str) BETWEEN (-9223372036854775808,'${first_format_month}') AND (9223372036854775807,'${last_format_month}') regions 10;"

echo `mysql -h ${tidb_host} -P ${tidb_port}  -u ${tidb_user} -p${tidb_password}  \
-e "split partition table ${tidb_db}.${tidb_tb} PARTITION ($partition_str) BETWEEN (-9223372036854775808,'${first_format_month}') AND (9223372036854775807,'${last_format_month}') regions 10;"`

if [ $? -ne 0 ];then
    echo -e "ERROR: Tidb数据删除失败 \n"
    exit -1
fi


# +-------------------------
# |   数据导入
# +-------------------------

sqoop export \
-Dsqoop.export.records.per.statement=10 \
-Dsqoop.export.statements.per.transaction=10 \
--connect jdbc:mysql://${tidb_host}:${tidb_port}/${tidb_db}?useUnicode=true \
--username ${tidb_user} \
--password ${tidb_password} \
--table ${tidb_tb} \
--hcatalog-database ${hive_db} \
--hcatalog-table ${hive_tbl} \
-m 30

if [ $? -ne 0 ];then
  echo -e "ERROR: 数据导入SQL加载失败 \n"
  exit -1
else
  echo -e "Execution sqoop Hive to Tidb, tableName: ${tidb_tb}  \n"
fi
